package cn.lingyangwl.agile.food.core.service.impl;

import cn.binarywang.wx.miniapp.api.WxMaQrcodeService;
import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.lingyangwl.agile.dynamic.client.AbstractClientProcess;
import cn.lingyangwl.agile.dynamic.client.wx.WxMaWrapClient;
import cn.lingyangwl.agile.model.BasePlusPO;
import cn.lingyangwl.agile.model.enums.ConfigDefineEnum;
import cn.lingyangwl.agile.model.module.tenant.TenantContext;
import cn.lingyangwl.agile.food.core.assembly.TableCategoryAssembly;
import cn.lingyangwl.agile.food.core.mapper.TableCategoryMapper;
import cn.lingyangwl.agile.food.model.entity.ShopTableCategory;
import cn.lingyangwl.agile.food.model.rqrs.table.TableCategoryResp;
import cn.lingyangwl.agile.food.model.rqrs.table.TableCategorySaveReq;
import cn.lingyangwl.agile.food.model.rqrs.table.TableCategoryUpdateReq;
import cn.lingyangwl.agile.food.core.service.TableCategoryService;
import cn.lingyangwl.framework.tool.core.exception.BizException;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.error.WxErrorException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

import static cn.lingyangwl.agile.food.model.constants.ShopCons.QrCodeType;

/**
 * 桌台分类
 *
 * @author shenguangyang
 */
@Slf4j
@Service
public class TableCategoryServiceImpl extends ServiceImpl<TableCategoryMapper, ShopTableCategory>
    implements IService<ShopTableCategory>, TableCategoryService {
    @Resource
    private TableCategoryAssembly tableCategoryAssembly;

    @Override
    public List<TableCategoryResp> listCategory() {
    	return this.list(ShopTableCategory.lqw().orderByDesc(BasePlusPO::getCreatedAt)).stream()
    	        .map(tableCategoryAssembly::toTableCategoryResp).collect(Collectors.toList());
    }

    @Override
    public boolean saveCategory(TableCategorySaveReq req) {
        ShopTableCategory entity = tableCategoryAssembly.toTableCategory(req);
    	return this.save(entity);
    }

    @Override
    public boolean updateCategory(TableCategoryUpdateReq req) {
        ShopTableCategory entity = tableCategoryAssembly.toTableCategory(req);
    	return this.updateById(entity);
    }

    @Override
    public boolean deleteCategory(Long id) {
    	return this.removeById(id);
    }
    
    @Override
    public boolean batchDeleteCategory(List<Long> ids) {
    	return this.removeByIds(ids);
    }

    @Override
    public byte[] genQrcode(Long id, String qrcodeType) {
        String tenantId = TenantContext.get().getTenantId();
        try {
            // 微信小程序
            if (QrCodeType.WEIXIN_MA.equals(qrcodeType)) {
                WxMaWrapClient client = (WxMaWrapClient) AbstractClientProcess.getDynamicClient(ConfigDefineEnum.WX_MA);
                if (Objects.isNull(client)) {
                    throw new BizException("请到总店配置小程序参数");
                }
                WxMaService wxMaService = client.getWxMaService();

                String pageUrl = "pages/index/index";
                String scene = "1047";
                WxMaQrcodeService qrcodeService = wxMaService.getQrcodeService();
                return qrcodeService.createWxaCodeUnlimitBytes(
                        scene, pageUrl, true, "develop", 430,
                        true, null, true);
            } else if (QrCodeType.H5.equals(qrcodeType)) {

            }
        } catch (BizException e) {
            throw e;
        } catch (WxErrorException e) {
            log.error("error: {}", e.getError().getJson());
            throw new BizException("请求微信失败 [{}]", e.getError().getErrorMsg());
        } catch (Exception e) {
            log.error("error: ", e);
            throw new BizException("生成二维码失败, 请联系提供商");
        }
        throw new BizException("未知二维码类型");
    }
}
